package com.summer.ioc.util;

/**
 * @author xiaoye
 * @create 2021-04-29 15:34
 */
public class StringUtils {

    static String emailRegex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    static String codeRegex = "^[a-z0-9A-Z]+$";
    static String phoneRegex = "(\\+\\d+)?1[3458]\\d{9}$";
    public static boolean checkStrIsNum(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    public int checkUsernameType(String username) {
        return username.matches(phoneRegex)?3:
                username.matches(codeRegex)?1:
                        username.matches(emailRegex)?1:null;
    }
    public static boolean isPhone(String str) {
        return str.startsWith("+") || !str.contains("@");
    }

    public static String castUnderlineToCamel(String str)
    {
        String[] strs = str.split("_");

        StringBuilder builder = new StringBuilder();
        for (String s : strs) {
            builder.append(firstLetterToUpperCase(s));
        }
        return builder.toString();
    }

    public static String castCamelCaseToUnderline(String str)
    {
        char[] chars = str.toCharArray();
        String result = "";
        for (char i : chars)
        {
            if (i >= 'A' && i <= 'Z')
            {
                result += "_" + String.valueOf(i).toLowerCase();
            }
            else
            {
                result += i;
            }
        }
        if (result.startsWith("_"))
        {
            result = result.substring(1);
        }
        return result;
    }

    public static String firstLetterToUpperCase(String str)
    {
        if (str.charAt(0) >= 'a' && str.charAt(0) <= 'z')
        {
            str = str.replaceFirst(String.valueOf(str.charAt(0)),String.valueOf(str.charAt(0)).toUpperCase());
        }
        return str;
    }

    public static String firstLetterToLowerCase(String str) {
        if (str.charAt(0) >= 'A' && str.charAt(0) <= 'Z')
        {
            str = str.replaceFirst(String.valueOf(str.charAt(0)),String.valueOf(str.charAt(0)).toLowerCase());
        }
        return str;
    }

    public static boolean hasLength(String str)
    {
        return str != null && str.length() > 0;
    }

    public static boolean hasText(String str)
    {
        return str != null && str.trim().length() > 0;
    }


}
